package com.example.demo.utils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;

public class ImageDisplayDialog extends JDialog {
    public ImageDisplayDialog(Frame owner, String imagePath) {
        super(owner, "Dependency Graph", true);
        setLayout(new BorderLayout());

        try {
            File imageFile = new File(imagePath);
            if (!imageFile.exists()) {
                JOptionPane.showMessageDialog(owner, "Image file does not exist: " + imagePath,
                        "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }

            ImageIcon icon = new ImageIcon(ImageIO.read(imageFile));
            JLabel label = new JLabel(icon);
            add(label, BorderLayout.CENTER);

            pack();  // 根据组件内容调整窗口大小
            setLocationRelativeTo(owner);  // 窗口居中显示
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            setVisible(true);
            System.out.println("Load image in " + imagePath);
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(owner, "Failed to load the image: " + e.getMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
